﻿using System.Collections;
using System.Collections.Generic;

using UnityEngine;

/// <summary>
/// 资源管理，提供各项本地资源，包括物体对象和UI等的解码和加载功能。
/// </summary>
public class ResourceManager
{
    /// <summary>
    /// Loads the specified path.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path">The path.</param>
    /// <param name="instance">if set to <c>true</c> [instance].</param>
    /// <returns></returns>
    public static T Load<T>(string path, bool instance = false) where T : UnityEngine.Object
    {
        T asset = Resources.Load<T>(path) as T;
        if (asset != null && instance)
        {
            return Object.Instantiate(asset);
        }
        return asset;
    }

    /// <summary>
    /// 从文件中实例化预设到游戏物体。
    /// </summary>
    /// <param name="path">预设目录。</param>
    /// <param name="parent">设置的父对象。</param>
    /// <returns></returns>
    public static GameObject Instantiate(string path, Transform parent = null)
    {
        GameObject asset = Load<GameObject>(path);
        GameObject go = null;
        if (asset != null)
        {
            go = GameObject.Instantiate(asset, parent) as GameObject;
        }
        return go;
    }
}